package org.alino.core.shiro.filter;

import org.alino.core.utils.LoggerUtils;
import org.alino.core.utils.StringUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Desc 角色判断校验
 * @Author by Brant
 * @Date 2017/1/13
 */
public class RoleFilter extends RolesAuthorizationFilter {
    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if(StringUtils.isBlank(mappedValue)){
            return false;
        }
        String[] arra = (String[])mappedValue;
        if(arra.length==0){
            LoggerUtils.debug(getClass(), "权限没有初始化");
        }
        Subject subject = getSubject(request, response);
        for (String role : arra) {
            LoggerUtils.debug(getClass(), "角色：%s",role);
            if(subject.hasRole("role:" + role)){
                return true;
            }
        }
        return false;
    }
}
